package com.rt.code.controller;

import com.rt.code.dto.A1templatetableDto;
import com.rt.code.dto.PageDto;
import com.rt.code.service.intf.IA1templatetableService;
import com.rt.tmpt.dto.Result;
import com.rt.tmpt.utils.StringUtils;
import com.rt.tmpt.utils.WebUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@Api(tags = "模板表-控制类")
@Controller
@RequestMapping("/admin123/coding/a1templatetable")
public class A1templatetableAction{

	@Resource
	private IA1templatetableService a1templatetableService;


	/**
	* 分页查询列表
	* @return
	*/
	@GetMapping(value = "/page")
	@ResponseBody
	public Result queryListBySearch(@ModelAttribute A1templatetableDto a1templatetableDto,
                                    @ModelAttribute PageDto pageDto){
		Result<A1templatetableDto> result = new Result<>();
		try {

			//查询参数
			//分页/排序查询条件
			//默认主键desc
			pageDto.configDefaultValue();

			//查询条件
			String tablename = a1templatetableDto.getTablename();
			a1templatetableDto.setTablename(tablename!=null?"%"+tablename+"%":null);
			String tablenameshow = a1templatetableDto.getTablenameshow();
			a1templatetableDto.setTablenameshow(tablenameshow!=null?"%"+tablenameshow+"%":null);

			//调用远程服务
			result =  a1templatetableService.pagination(a1templatetableDto,pageDto);

		} catch (Exception e) {
			e.printStackTrace();
			result.fault(e.getLocalizedMessage());
		}
		return result;
	}

	@ApiOperation("selectById")
	@GetMapping(value = "/selectById")
	@ResponseBody
	public Result selectById(Long id){
		Result<A1templatetableDto> result = new Result<>();
		try {
			//调用远程服务
			result =  a1templatetableService.selectById(id);
		} catch (Exception e) {
			e.printStackTrace();
			result.fault(e.getLocalizedMessage());
		}
		return result;
	}

	/**
	* 伪删除数据
	* @return
	*/
	@PostMapping(value = "/a1templatetableAjax_removeData", produces = "application/json; charset=utf-8")
	@ResponseBody
	public Result removeData(String _userName, String id){
		Result result = new Result();
		try {
			//调用远程服务
			result =  a1templatetableService.deleteById(Long.valueOf(id),_userName);

		} catch (Exception e) {
			e.printStackTrace();
			result.fault("error:系统出错:"+e.getLocalizedMessage());
		}

		return result;
	}


	/**
	* 编辑数据
	* @return
	*/
	@PostMapping(value = "/a1templatetableAjax_editData", produces = "application/json; charset=utf-8")
	@ResponseBody
	public Result editData(@RequestBody A1templatetableDto a1templatetableDto,
                           HttpServletRequest request,
						   String _userId, String _userName){
		Result<A1templatetableDto> result = new Result();
		try {
			HttpSession session = request.getSession();

			//参数
			a1templatetableDto.setModifymanId(_userId);
			a1templatetableDto.setModifyman(_userName);
			a1templatetableDto.setCompanyId(WebUtils.getCompanyId(session));

			//调用远程服务
			result =  a1templatetableService.editData(a1templatetableDto);

		} catch (Exception e) {
			e.printStackTrace();
			result.fault("error:系统出错:"+e.getLocalizedMessage());
		}

		return result;
	}

	/**
	 * edit config tree json
	 * @param a1templatetableDto
	 * Long a1templatetableId, String treeJson
	 * @return
	 */
	@PostMapping(value = "/editTreeJsonById", produces = "application/json; charset=utf-8")
	@ResponseBody
	public Result editTreeJsonById(@RequestBody A1templatetableDto a1templatetableDto,
						   String _userId, String _userName){
		Result<A1templatetableDto> result = new Result();
		try {
			//参数
			a1templatetableDto.setModifymanId(_userId);
			a1templatetableDto.setModifyman(_userName);
			//调用远程服务
			result =  a1templatetableService.editTreeJsonById(a1templatetableDto);
		} catch (Exception e) {
			e.printStackTrace();
			result.fault("error:系统出错:"+e.getLocalizedMessage());
		}

		return result;
	}

	/**
	* 添加数据
	* @return
	*/
	@PostMapping(value = "/a1templatetableAjax_addData", produces = "application/json; charset=utf-8")
	@ResponseBody
	public Result addData(@RequestBody A1templatetableDto a1templatetableDto,
                          HttpServletRequest request,
						  String _userId, String _userName){
		Result<A1templatetableDto> result = new Result();
		try {
			HttpSession session = request.getSession();

			//插入参数
			a1templatetableDto.setCreatemanId(_userId);
			a1templatetableDto.setCreateman(_userName);
			a1templatetableDto.setCompanyId(WebUtils.getCompanyId(session));
			//插入数据

			//调用远程服务
			result =  a1templatetableService.addData(a1templatetableDto);

		} catch (Exception e) {
			e.printStackTrace();
			result.fault("error:系统出错:"+e.getLocalizedMessage());
		}
		return result;
	}



}
